﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using YGWebMobileApi.Data;
using OneMile.DAL;
using OneMile.Common;
using OneMile.Entity.Model;
using System.Text;
using OneMile.BLL;

namespace YGWebMobileApi.APIs
{
     [MobileAPI(Code = "MC0330", Name = "用户获取地址列表")]
    public class UGetAddressListByUserId : BaseAPI, IProcessMessage
    {
         private BLLUUserAddress bllAdr;
         protected BLLUUserAddress BLLAdr
        {
            get
            {
                if (bllAdr == null)
                {
                    bllAdr = BLLFactory.getInstance().CreateUUserAddressBLL();
                }
                return bllAdr;
            }
        }
        #region IProcessMessage 成员

        public MobileRspMsg ProcessMessage(MobileReqMsg reqMsg)
        {
            try
            {
                MobileRspMsg rspMsg = new MobileRspMsg(reqMsg);

                string UserId = reqMsg.Body.GetFieldValue("UserId");

                if (String.IsNullOrEmpty(UserId))
                {
                    rspMsg.Head.RspCode = MessageCode.Paramter_Empty.Code;
                    rspMsg.Head.RspMsg = MessageCode.Paramter_Empty.Msg;
                    return rspMsg;
                }

                List<EUserAddress> list = (List<EUserAddress>)BLLAdr.GetUserAddressListByUserId(UserId);
        
                for (int i = 0; i < list.Count; i++)
                {
                    var fll = rspMsg.Body.AddFieldList("Address");
                    fll.AddField("UserAddressId", list[i].UserAddressId.TString());
                    fll.AddField("AddressName", list[i].AddressName);
                    fll.AddField("UserId", list[i].UserId.TString());
                    fll.AddField("Address", list[i].Address);
                    fll.AddField("Consignee", list[i].Consignee);
                    fll.AddField("Mobile", list[i].Mobile);
                }

                rspMsg.Head.RspCode = MessageCode.Success.Code;
                rspMsg.Head.RspMsg = MessageCode.Success.Msg;

                return rspMsg;
            }
            catch (Exception e)
            {
                ErrorLogWriter.Write("UGetAddressListByUserId-" + e);
                throw e;
            }
        }

        #endregion
    }
}